package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.entity.Experience;
import com.a01_2021.thousandstimes.general.entity.ExperienceDetail;
import com.a01_2021.thousandstimes.general.entity.PersonnelInfomation;
import com.a01_2021.thousandstimes.general.entity.SysUser;
import com.a01_2021.thousandstimes.general.service.*;
import com.a01_2021.thousandstimes.instance.entity.Dimission;
import com.a01_2021.thousandstimes.instance.mapper.DimissionMapper;
import com.a01_2021.thousandstimes.instance.service.IDimissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@Service
public class DimissionServiceImpl extends ServiceImpl<DimissionMapper, Dimission> implements IDimissionService {

    @Resource
    DimissionMapper dimissionMapper;

    @Resource
    SysUserService sysUserService;

    @Resource
    ExperienceService experienceService;

    @Resource
    IExperienceDetailService experienceDetailService;

    @Resource
    PersonnelInfomationService personnelInfomationService;

    @Resource
    SysRoleService sysRoleService;


    /**
     * 离职申请
     *
     * @param userId 用户id
     * @param reason 原因
     * @return Dimission实体
     */
    @Override
    public Dimission dimission(long userId, String reason) throws CustomException {
        if (this.isDimission(userId)) {
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "不允许重复申请离职，请等待上个离职处理");
        }
        Dimission dimission = new Dimission(null, userId, "unHandle", LocalDateTime.now(), null, null, reason);
        dimissionMapper.insert(dimission);
        return dimission;
    }

    /**
     * 判断是否有有效离职申请
     *
     * @param userId 用户id
     * @return 返还是否申请过离职
     * @throws CustomException 当用户不存在时抛出该异常
     */
    @Override
    public boolean isDimission(long userId) throws CustomException {
        if (!sysUserService.isUser(userId)) {
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "找不到该用户");
        }
        QueryWrapper<Dimission> dimissionQueryWrapper = new QueryWrapper<>();
        dimissionQueryWrapper.eq("user_id", userId);
        dimissionQueryWrapper.eq(false, "stats", "false");
        int count = dimissionMapper.selectCount(dimissionQueryWrapper);
        return count != 0;
    }

    /**
     * 用于获取未处理的申请
     *
     * @return List离职申请列表
     */
    @Override
    public List<Dimission> getUnHandle() {
        QueryWrapper<Dimission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stats", "unHandle");
        return dimissionMapper.selectList(queryWrapper);
    }

    /**
     * 用于同意员工离职申请
     *
     * @param dimissionId 离职id
     * @param instanceId  实例id
     * @return 离职实体类
     * @throws CustomException 当离职申请不存在时抛出该异常
     */
    @Override
    public Dimission agree(long dimissionId, long instanceId) throws CustomException {
        Dimission dimission = dimissionMapper.selectById(dimissionId);
        if (dimission == null) {
            throw new CustomException(CustomExceptionType.DIMISSION_NOT_FOUND_ERROR, "该离职申请不存在");
        }
        dimission.setStats("true");
        dimissionMapper.updateById(dimission);
        //以下离职数据处理
        //将系统内个人信息移动到主数据库
        SysUser user = sysUserService.getById(dimission.getUserId());//查询系统内对应用户
        PersonnelInfomation personnelInfomation = personnelInfomationService.getByUserIdOriginal(dimission.getUserId());//查询系统内的个人信息
        long masterUserId = personnelInfomationService.updatePersonalInformationToMaster(personnelInfomation, user);//获取新实例中的用户id
        //用于将员工的经历插入主数据库
        List<Experience> experienceList = experienceService.getFromInstanceIdAndUserId(dimission.getUserId(), instanceId);
        for (Experience item : experienceList) {
            //更新用户id指向
            item.setUserId(masterUserId);
        }
        Map<Long, Long> map = experienceService.addIntoMaster(experienceList);//返还Experience旧id-新id
        //用于将员工的信息经历插入数据库
        List<ExperienceDetail> experienceDetailList = experienceDetailService.getFromInstanceIdAndUserId(dimission.getUserId(), instanceId);
        for (ExperienceDetail item : experienceDetailList) {
            //更新用户id指向
            item.setUserId(masterUserId);
        }
        experienceDetailService.insertIntoMaster(experienceDetailList, map);
        //删除用户对应角色
        sysRoleService.dropByUserId(dimission.getUserId());
        return dimission;
    }

    /**
     * 用于不同意员工离职申请
     *
     * @param dimissionId 离职id
     * @return 离职实体类
     * @throws CustomException 当离职申请不存在时抛出该异常
     */
    @Override
    public Dimission disagree(long dimissionId) throws CustomException {
        Dimission dimission = dimissionMapper.selectById(dimissionId);
        if (dimission == null) {
            throw new CustomException(CustomExceptionType.DIMISSION_NOT_FOUND_ERROR, "该离职申请不存在");
        }
        dimission.setStats("false");
        dimissionMapper.updateById(dimission);
        return dimission;
    }

    /**
     * 用于获取员工的离职申请
     *
     * @param userId 用户id
     * @return List离职申请列表
     */
    @Override
    public List<Dimission> getMyDimission(long userId) {
        QueryWrapper<Dimission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return dimissionMapper.selectList(queryWrapper);
    }
}
